---
title: ""
output: pdf_document
geometry: "top=1in, bottom=1in, left=1in, right=1in"
header-includes:
    - \usepackage{caption}
    - \usepackage{lscape}
---

\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}

\captionsetup[table]{labelformat=empty}

```{r setup, include=FALSE}
if (!require("pacman")) install.packages("pacman", repos = "http://cran.us.r-project.org"); library(pacman);
p_load(tidyverse, arrow, yaml, here, argparse, glue, knitr, kableExtra, scales)


# Working directory and command line argument setup
current_script = "generate_tables_and_figures/gen_time_of_day_outputs/src/print_time_of_day_outcomes.Rmd"
here::i_am(current_script)
source(here("R", "project_functions.R"))

task_dir = here(dirname(dirname(file.path(current_script))))

knitr::opts_knit$set(root.dir = task_dir)
cl_args <- parse_make_args(c(
  "RMD_FILE",
  "CONFIG_FILE",
  "TIME_OF_DAY_REG_OUTPUT",
  "OUTCOME_LABELS_FILE",
  "OUT_MAIN_FILE",
  "OUT_APPENDIX_FILE"
), inter_active = TRUE, task_dir = task_dir)

knitr_setup()
  
timestamp <- get_timestamp()
  


```

\blandscape

```{r}
source('../R/table_functions.R')
table_config <- read_yaml('../hand/tables_config.yaml')
beta_se_digits <- table_config$digits$beta_se
pval_digits <- table_config$digits$pval


config <- read_yaml(cl_args$CONFIG_FILE)

table <- read_csv(cl_args$TIME_OF_DAY_REG_OUTPUT) #cl_args$TIME_OF_DAY_REG_OUTPUT

ylabels <- read_csv(cl_args$OUTCOME_LABELS_FILE) %>%
   select(outcome = value, label)

main_vars <- config$main_vars %>%
  as_tibble() %>%
  mutate(outcome_order = 1:n())

appendix_vars <- config$appendix_vars %>%
  as_tibble() %>%
  mutate(outcome_order = 1:n())

  
table_wide <- table %>%
  select(outcome, control_mean, beta_itt, stderr_itt, pval_itt) %>%
  mutate(time_window = case_when(outcome %>% str_ends("not_workhours_or_weekend") ~ "not_workhours_or_weekend",
                                 outcome %>% str_ends("workhours") ~ "workhours",
                                 outcome %>% str_ends("weekend") ~ "weekend"),
         outcome = outcome %>% str_remove(paste0("_",time_window))) %>%
  pivot_wider(names_from = time_window, values_from = c(control_mean, beta_itt, stderr_itt, pval_itt)) %>%
  mutate(across(contains("stderr") | contains("beta") | contains("control_mean"), ~fmt(.x, beta_se_digits)),
         across(contains("pval"), ~fmt(.x, pval_digits))) 

main_table <- table_wide %>%
  inner_join(main_vars, c("outcome" = "value")) %>%
  arrange(outcome_order) %>%
  stack_stderrs_under_betas() %>%
  left_join(ylabels) %>%
  select(label, control_mean_workhours, beta_itt_workhours, pval_itt_workhours, control_mean_weekend, beta_itt_weekend, pval_itt_weekend,
         control_mean_not_workhours_or_weekend, beta_itt_not_workhours_or_weekend, pval_itt_not_workhours_or_weekend)

appendix_table <- table_wide %>%
  inner_join(appendix_vars, c("outcome" = "value")) %>%
  arrange(outcome_order) %>%
  stack_stderrs_under_betas() %>%
  left_join(ylabels) %>%
  select(label, control_mean_workhours, beta_itt_workhours, pval_itt_workhours, control_mean_weekend, beta_itt_weekend, pval_itt_weekend,
         control_mean_not_workhours_or_weekend, beta_itt_not_workhours_or_weekend, pval_itt_not_workhours_or_weekend)

```

```{r}
#primary table
table <- main_table %>%
  kable(booktabs=T, longtable=T,
        col.names = c("", rep(c("CM", "ITT", "P-value"), 3)),
        align=c("l", rep("c", 9)),
         linesep = c(rep("",5), "\\addlinespace")) %>% 
  kable_styling(font_size = 10) %>%
  #add_indent(c(1:40)) %>% 
  column_spec(1, width="20em") %>%
  column_spec(2:10, width="3.5em") %>% 
  group_rows("Primary Outcome Components, Counts", 1, 6) %>%
  group_rows("All Events, Counts", 7, 10) %>%
  add_indent(c(1:10)) %>% 
  add_header_above(c(" " = 1, "Work Hours\n(Mon-Fri 8am-6pm)" = 3, "Weekend\n(Fri 6pm - Sun 11:59pm)" = 3, "Weekday Mornings and Nights\n(Mon-Fri 6pm-8am)" = 3), bold = F)


table

writeLines(table, cl_args$OUT_MAIN_FILE)
writeLines(table, gsub('up_to_date', timestamp, cl_args$OUT_MAIN_FILE))


table <- appendix_table %>%
  kable(booktabs=T, longtable=T,
        col.names = c("", rep(c("CM", "ITT", "P-value"), 3)),
        align=c("l", rep("c", 9)),
        linesep = "") %>% 
  kable_styling(font_size = 10) %>%
  #add_indent(c(1:40)) %>% 
  column_spec(1, width="18em") %>%
  column_spec(2:10, width="3.8em") %>% 
  group_rows("Other Arrest & Victimization Counts", 1, 10) %>%
  add_indent(c(1:10)) %>% 
  add_header_above(c(" " = 1, "Work Hours\n(Mon-Fri 8am-6pm)" = 3, "Weekend\n(Fri 6pm - Sun 11:59pm)" = 3, "Weekday Mornings and Nights\n(Mon-Fri 6pm-8am)" = 3), bold = F)
  

table



writeLines(table, cl_args$OUT_APPENDIX_FILE)
writeLines(table, gsub('up_to_date', timestamp, cl_args$OUT_APPENDIX_FILE))


```

\elandscape
